package com.zhaoyu.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhaoyu.admin.mapper.AdChannelMapper;
import com.zhaoyu.admin.service.AdChannelService;
import com.zhaoyu.model.admin.dtos.ChannelDto;
import com.zhaoyu.model.admin.pojos.AdChannel;
import com.zhaoyu.model.common.dtos.PageResponseResult;
import com.zhaoyu.model.common.dtos.ResponseResult;
import com.zhaoyu.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * IService 实现类（泛型：M 是 mapper 对象，T 是实体）
 */
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    @Override
    public ResponseResult findByNameAndPage(ChannelDto channelDto) {
        /**
         * 请求参数是null，默认查第一页
         */
        if (channelDto == null){
            channelDto = new ChannelDto();
            // return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        /**
         * 获取默认的分页信息
         */
        channelDto.checkParam();
        /**
         * 分页+模糊查询
         * like(column,val)
         * column 字段(实体类) val 值(DTO)
         */
        Page page =  new Page<AdChannel>(channelDto.getPage(),channelDto.getSize());
        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(channelDto.getName())){ // 校验参数 isBlank(null和"") isEmpty(null)
            wrapper.like(AdChannel::getName,channelDto.getName()); // AdChannel::getName：频道名称
        }
        IPage result = page(page,wrapper);
        /**
         * 当前页(默认第一页)：getCurrent
         * 当前页总页数：getSize
         * 总条数：getTotal
         * 分页对象记录列表：getRecords
         * 封装结果
         */
        ResponseResult responseResult = new PageResponseResult((int) result.getCurrent(),(int) result.getSize(),(int) result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        /**
         * 1、检查参数
         */
        if (adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID );
        }
        /**
         * 2、保存
         */
        adChannel.setCreatedTime(new Date());
        save(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult edit(AdChannel adChannel) {
        /**
         * 1、检查参数
         */
        if (adChannel == null || adChannel.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID );
        }
        /**
         * 2、修改
         */
        updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        /**
         * 1、检查参数
         */
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        /**
         * 2、判断当前频道是否存在 和 是否有效
         */
        AdChannel adChannel = getById(id);
        if (adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        /**
         * 3、频道有效不能删除
         */
        if (adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道有效不能删除!");
        }
        /**
         * 4、删除
         */
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}